/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __RGCPP_TOSTRING_H__
#define __RGCPP_TOSTRING_H__

#include <time.h>
#include <string>
#include <sstream>
#include <iostream>
#include <typeinfo>

#include "rgcpp/exceptions/RuntimeException.h"

namespace rgcpp
{

template<typename T>
inline std::string ToString(const T& x)
{
	std::ostringstream o;
	if (!(o << x))
		Throw(UnsupportedOperationException(std::string("ToString(") + typeid(x).name() + ")"));
	std::string result = o.str();
	return result;
}

inline std::string ToString(const time_t rawtime, const std::string& format)
{
	char buffer[128];
	struct tm* timeinfo = localtime(&rawtime);

	strftime(buffer,128,format.c_str(),timeinfo);

	return std::string(buffer);
}

inline std::string ToStringHumanReadable(const unsigned int size)
{
	if (size >= 1024 && size < 1048576)
		return ToString((size*10/1024)/10.0f)+"KB";

	if (size >= 1048576 && size < 1073741824)
		return ToString((size*10/1048576)/10.0f)+"MB";

	return ToString(size)+"B";
}

}

#endif
